package com.demo.apparel.service.web.Impl;

import com.demo.apparel.dto.ClothesAdminDTO;
import com.demo.apparel.dto.web.ClothesPageQueryDTO;
import com.demo.apparel.entity.Category;
import com.demo.apparel.entity.Clothes;
import com.demo.apparel.mapper.uniapp.WxCategoryMapper;
import com.demo.apparel.mapper.web.ClothesAdminMapper;
import com.demo.apparel.mapper.web.WebCategoryMapper;
import com.demo.apparel.service.web.ClothesAdminService;
import com.demo.apparel.vo.CategoryAdminVO;
import com.demo.apparel.vo.ClothesGroupAdminVO;
import com.demo.apparel.vo.ClothesSimpleVO;
import com.demo.apparel.vo.PageVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class ClothesAdminServiceImpl implements ClothesAdminService {

    @Autowired
    private ClothesAdminMapper clothesAdminMapper;

    @Autowired
    private WebCategoryMapper webCategoryMapper;
    /**
    /**
     *
     * @return
     */
    @Override
    public List<CategoryAdminVO> getClothesGroupedByCategory() {
        List<Category> categories = webCategoryMapper.selectAllCategories();
        List<CategoryAdminVO> result = new ArrayList<>();

        for (Category category : categories) {
            List<String> subNames = clothesAdminMapper.selectSubNames(category.getCategory());
            List<ClothesGroupAdminVO> typeList = new ArrayList<>();
            int total = 0;
            int index = 1;

            for (String name : subNames) {
                List<ClothesSimpleVO> clothesList = clothesAdminMapper.selectClothesByCategoryAndName(category.getCategory(), name);
                ClothesGroupAdminVO groupVO = new ClothesGroupAdminVO();
                groupVO.setIndexId(index++);
                groupVO.setName(name);
                groupVO.setTotal(clothesList.size());
                groupVO.setClothes(clothesList);
                typeList.add(groupVO);
                total += clothesList.size();
            }

            CategoryAdminVO vo = new CategoryAdminVO();
            vo.setId(category.getId());
            vo.setCategoryName(category.getCategoryName());
            vo.setTotal(total);
            vo.setType(typeList);
            result.add(vo);
        }

        return result;
    }

//    /**
//     *
//     * @param dto
//     * @return
//     */
//        @Override
//        public PageVO<ClothesAdminDTO> pageQuery(ClothesPageQueryDTO dto) {
//            int pageNum = dto.getPageNum() != null ? dto.getPageNum() : 1;
//            int pageSize = dto.getPageSize() != null ? dto.getPageSize() : 10;
//            int offset = (pageNum - 1) * pageSize;
//
//            List<ClothesAdminDTO> records = clothesAdminMapper.selectClothesPage(
//                    dto.getName(), dto.getCategory(), offset, pageSize
//            );
//            long total = clothesAdminMapper.countClothes(dto.getName(), dto.getCategory());
//
//            return new PageVO<>(records, total, pageNum, pageSize);
//        }

    @Override
    public PageVO<ClothesAdminDTO> pageQuery(ClothesPageQueryDTO dto) {
        int pageNum = dto.getPageNum() != null ? dto.getPageNum() : 1;
        int pageSize = dto.getPageSize() != null ? dto.getPageSize() : 10;
        int offset = (pageNum - 1) * pageSize;

        List<ClothesAdminDTO> records = clothesAdminMapper.selectClothesPage(
                dto.getName(), dto.getCategory(), dto.getUsername(), offset, pageSize
        );
        long total = clothesAdminMapper.countClothes(dto.getName(), dto.getCategory(), dto.getUsername());

        return new PageVO<>(records, total, pageNum, pageSize);
    }


}
